import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.ClassCastException;
import java.awt.image.*;

//GUI of BattleShip

public class Window extends JFrame
{
	private JMenuItem newGame, quit, about;
	private computerai cai = new computerai();
	private JMenuBar bar;
	private JMenu fileMenu, optionMenu, challenge;
	private JCheckBoxMenuItem easy, normal, impossible;
	private JPanel fieldPanel, logoPanel, buttonPanel,	 mainPanel;
	private JLabel logoLabel;
	private Board player, computer;
	public static int   CARRIER = 0,
						BATTLESHIP = 1,
						SUBMARINE = 2,
						DESTROYER = 3,
						PATROL = 5;
	public int selectedShip = 6;
	public ImageIcon 	logo,
						cLogo, bLogo, sLogo, dLogo, pLogo,
						cVLogo, bVLogo, sVLogo, dVLogo,pVLogo,
						fireLogo,
						missLogo;
	public ImageIcon[] 	cIcon, bIcon, dIcon, sIcon, pIcon,
						cVIcon, bVIcon, dVIcon, sVcon, pVIcon;
						
	public int	selectedShipSize = 0;
	public int swap = 0;
	public boolean setting = false, destroyerSet = false;
	public int direction = 1; 				//1 is horizontal, 2 is vertical
	public BoardGUI playerBoard;
	public BoardGUI computerBoard;
	
	public Ship[] ship, boat;
	
	private JButton carrierB, destroyerB, battleshipB, submarineB, patrolB, autoB, manualB;
	
	private Container c;
	
	private int difficulty = 1;				//easy is 0, normal is 1, impossible is 2!
	public int startup,hitx, hity, ohitx, ohity;						//startup is 0, new game is 1
	
	public Window()
	{
		super("BattleShip");
		c = getContentPane();
		c.setLayout(new BorderLayout());
		player = new Board(false);
		computer = new Board(true);
		startup = 0;
		
		ItemHandler itemHandler = new ItemHandler();

		setJMenuBar(bar = new JMenuBar());

		(fileMenu = new JMenu("File")).setMnemonic('F');		// This is the File Menu
		
		newGame = new JMenuItem("New Game...",'G');
		newGame.addActionListener(itemHandler);
		fileMenu.add(newGame);

		fileMenu.addSeparator();
		about = new JMenuItem("About...",'A');
		about.addActionListener(itemHandler);
		fileMenu.add(about);

		quit = new JMenuItem("Exit",'x');
		quit.addActionListener(itemHandler);
		fileMenu.add(quit);

		(optionMenu = new JMenu("Options")).setMnemonic('O');	 // This is the Console Menu

		challenge = new JMenu("Game Difficulty");

		(easy = new JCheckBoxMenuItem("Easy")).addActionListener(itemHandler);
		(normal = new JCheckBoxMenuItem("Normal")).setSelected(true);
		normal.addActionListener(itemHandler);
		(impossible = new JCheckBoxMenuItem("Impossible")).addActionListener(itemHandler);

		challenge.add(easy);
		challenge.add(normal);
		challenge.add(impossible);
		optionMenu.add(challenge);
		
		bar.add(fileMenu);
		bar.add(optionMenu);
		
		fieldPanel = new JPanel();
		fieldPanel.setBorder(new TitledBorder("Battleship"));
		fieldPanel.setLayout(new BorderLayout());
		
		logo = new ImageIcon("graphics/logo.jpg");
		logoPanel = new JPanel();
		logoLabel = new JLabel(logo);
		logoLabel.setPreferredSize(new Dimension(logo.getIconWidth(),logo.getIconHeight()));
		logoLabel.setOpaque(true);
		logoLabel.setBackground(Color.BLUE);
		logoLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		logoLabel.addMouseListener(itemHandler);
		logoPanel.add(logoLabel);
		fieldPanel.add(logoPanel, BorderLayout.CENTER);
		
		c.add(fieldPanel, BorderLayout.CENTER);
		
		(autoB = new JButton("Automatic")).addActionListener(itemHandler);
		(manualB = new JButton("Manual")).addActionListener(itemHandler);
		
		mainPanel = new JPanel();
		mainPanel.setBorder(new TitledBorder("Select Automatic or Manual Ship Placement."));
		mainPanel.add(autoB);
		mainPanel.add(manualB);
		
		c.add(mainPanel, BorderLayout.NORTH);
		
		cIcon = new ImageIcon[5];
		cLogo = new ImageIcon("graphics/carrier.gif");
		int w = cLogo.getIconWidth()/5;
		System.out.println(w);
		int h = cLogo.getIconHeight();
		System.out.println(h);
		BufferedImage buff = getBufferedImage(cLogo);
		
		for(int i = 0; i < cIcon.length; i++) {
			cIcon[i] = new ImageIcon(buff.getSubimage(i * 25, 0, h, h));
		}
		
		dLogo = new ImageIcon("graphics/destroyer.gif");
		bLogo = new ImageIcon("graphics/battleship.gif");
		sLogo = new ImageIcon("graphics/submarine.gif");
		pLogo = new ImageIcon("graphics/patrol.gif");
		cVLogo = new ImageIcon("graphics/carrierv.gif");
		dVLogo = new ImageIcon("graphics/destroyerv.gif");
		bVLogo = new ImageIcon("graphics/battleshipv.gif");
		sVLogo = new ImageIcon("graphics/submarinev.gif");
		pVLogo = new ImageIcon("graphics/patrolv.gif");
		missLogo = new ImageIcon("graphics/splash.gif");
		fireLogo = new ImageIcon("graphics/fire.gif");
		
		(carrierB = new JButton("Carrier", cLogo)).addActionListener(itemHandler);
		(destroyerB = new JButton("Destroyer", dLogo)).addActionListener(itemHandler);
		(battleshipB = new JButton("Battleship", bLogo)).addActionListener(itemHandler);
		(submarineB = new JButton("Submarine", sLogo)).addActionListener(itemHandler);
		(patrolB = new JButton("Patrol", pLogo)).addActionListener(itemHandler);
		//banana
		JPanel shipsTop = new JPanel(new GridLayout(1,2));
		shipsTop.add(carrierB);
		shipsTop.add(battleshipB);

		JPanel shipsBottom = new JPanel(new GridLayout(1,3));
		shipsBottom.add(destroyerB);
		shipsBottom.add(submarineB);
		shipsBottom.add(patrolB);

		JPanel ships = new JPanel(new GridLayout(2,1));

		ships.add(shipsTop);
		ships.add(shipsBottom);

		buttonPanel = new JPanel();
		buttonPanel.setLayout(new BorderLayout());
		buttonPanel.setBorder(new TitledBorder("Select A Ship To Place on the Home Field.  Right click to rotate."));
		buttonPanel.add(ships, BorderLayout.CENTER);
		
		c.add(buttonPanel, BorderLayout.SOUTH);
		buttonPanel.setVisible(false);
		
		newGame();
		startup = 1;
		
		setResizable(false);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		pack();
	}
	/*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
	public static void main(String [] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				new Window().setVisible(true);
			}
		});
	}
	
	public void newGame() {
		if(startup == 1) {
			playerBoard.setVisible(false);
			computerBoard.setVisible(false);
		}
		playerBoard = new BoardGUI();
		playerBoard.setBorder(new TitledBorder("Home Field"));
		computerBoard = new BoardGUI();
		computerBoard.setBorder(new TitledBorder("Opponent's Field"));
		fieldPanel.add(playerBoard, BorderLayout.EAST);
		fieldPanel.add(computerBoard, BorderLayout.WEST);
		player = new Board(false);
		computer = new Board(true);
		int[] info = new int[3];
		int[] shipSize = {5,4,3,3,2};
		ship  = new Ship[5];
		for(int i = 0; i < 5; i++) {
			info = computer.randomSetShip(shipSize[i]);
			ship[i] = new Ship(info[0], info[1], info[2], shipSize[i], 2);
		}
		autoB.setEnabled(true);
		manualB.setEnabled(true);
		carrierB.setEnabled(true);
		battleshipB.setEnabled(true);
		destroyerB.setEnabled(true);
		submarineB.setEnabled(true);
		patrolB.setEnabled(true);
		mainPanel.setVisible(true);
		buttonPanel.setVisible(false);
		pack();
	}
	public boolean isSunk(Ship ship) {
		//This function will return whether or not the ship has been sunk yet
		boolean isIt = true;
		if(ship.dir == 1) {
			for(int i=ship.startX; i<ship.startX + ship.length && i<10; i++) {
				if(ship.plyr==1 && player.checkSquare(i, ship.startY)!= Board.Square.hit.ordinal()) {
					isIt = false;
				}
				else if(computer.checkSquare(i, ship.startY) != Board.Square.hit.ordinal()) {
					isIt = false;
				}
			}
		}
		else {
			for(int i = ship.startY; i<ship.startY + ship.length && i<10; i++) {
				if(ship.plyr==1 && player.checkSquare(ship.startX, i)!= Board.Square.hit.ordinal()) {
					isIt = false;
				}
				else if(computer.checkSquare(ship.startX, i)!= Board.Square.hit.ordinal()) {
					isIt = false;
				}
			}
		}
		return isIt;
	}
	public BufferedImage getBufferedImage(ImageIcon source) {
		BufferedImage buffered = new BufferedImage(source.getIconWidth(), source.getIconHeight(), BufferedImage.TYPE_INT_RGB);
		Graphics2D g2 = buffered.createGraphics();
		g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g2.drawImage(source.getImage(), 0, 0, source.getIconWidth(), source.getIconHeight(), null);
		g2.dispose();
		return buffered;
	} 
	/*-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
	public class BoardGUI extends JPanel {
		private ItemHandler itemHandler = new ItemHandler();
		public JLabel[][] battleboard = new JLabel [10][10];
		private ImageIcon waterIcon;
		public BoardGUI() {
			waterIcon = new ImageIcon("graphics/water.gif");
			setLayout(new GridLayout(10,10));
			for (int r = 0; r < battleboard.length; r++){
				for(int c =0; c < battleboard[r].length; c++){
					battleboard[r][c] = new JLabel (waterIcon);//add water image
					battleboard[r][c].setPreferredSize(new Dimension(waterIcon.getIconWidth(),waterIcon.getIconHeight()));
					battleboard[r][c].setBackground(Color.BLUE);
					battleboard[r][c].setOpaque(true);
					battleboard[r][c].setBorder(BorderFactory.createLineBorder(Color.BLACK));
					battleboard[r][c].addMouseListener(itemHandler);
					add(battleboard[r][c]);
				}
			}
		}
		
		public void update() {
			for(int i = 0; i < 10; i++) {
				for(int j = 0; j < 10; j++) {
					if(player.checkSquare(i, j)==Board.Square.ship.ordinal()) {
						battleboard[i][j].setBackground(Color.gray);
						battleboard[i][j].setIcon(null);
					}
					else if(player.checkSquare(i, j)==Board.Square.hit.ordinal()) {
						battleboard[i][j].setIcon(fireLogo);
						battleboard[i][j].setBackground(Color.red);
					}
					else if(player.checkSquare(i, j)==Board.Square.miss.ordinal()) {
						battleboard[i][j].setBackground(new Color(135, 248, 242));
						battleboard[i][j].setIcon(missLogo);
					}
					else {
						battleboard[i][j].setIcon(waterIcon);
					}
				}
			}
		}
		
		
		/*public void update(int selectedShip, int selectedShipSize, int direction) {
			for(int i = 0; i < 10; i++) {
				for(int j = 0; j < 10; j++) {
					if(player.checkSquare(i, j)==Board.Square.ship.ordinal()) {
						if(direction == 1) {
							if(selectedShip == CARRIER) {
								for(int k = 0; k < selectedShipSize; k++) {
									battleboard[i][j].setIcon(cIcon[k]);
								}
							}
						}
					}
				}
			}
		}*/
		
		public void cUpdate() {
			for(int i = 0; i < 10; i++) {
				for(int j = 0; j < 10; j++) {
					if(computer.checkSquare(i, j)==Board.Square.ship.ordinal()) {
						battleboard[i][j].setBackground(Color.gray);
					}
					else if(computer.checkSquare(i, j)==Board.Square.hit.ordinal()) {
						battleboard[i][j].setIcon(fireLogo);
						battleboard[i][j].setBackground(Color.red);
					}
					else if(computer.checkSquare(i, j)==Board.Square.miss.ordinal()) {
						battleboard[i][j].setBackground(new Color(135, 248, 250));
						battleboard[i][j].setIcon(missLogo);
					}
					else {
						battleboard[i][j].setIcon(waterIcon);
						
					}
				}
			}
		
		}
	}
	/*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
	private class ItemHandler implements ActionListener, MouseListener {
		public void actionPerformed( ActionEvent e ) {
			
			if (e.getSource()==newGame) {
				String level = null;
				switch (difficulty) {
					case 0:	level = "Easy";			break;
					case 1:	level = "Normal";		break;
					case 2:	level = "Impossible";	break;
				}
				newGame();
			}
			if (e.getSource()==about) {
				JOptionPane.showMessageDialog(c,
					"Created by Group 2:\n"+
                    "Group Leader: Kholby Lawson \n"+
					"Ryan Vargas \n"+
                    "Phillip Green \n" +
					"Matthew Shaffer",
					"About BattleShip", JOptionPane.PLAIN_MESSAGE);
			}
			if (e.getSource()==quit) {  System.exit(0);	}
			if (e.getSource()==easy) {
				if (normal.isSelected() || impossible.isSelected()) {
					normal.setSelected(false);
					impossible.setSelected(false);
					difficulty = 0;
				}
				else easy.setSelected(true);
			}
			if (e.getSource()==normal) {
				if (easy.isSelected() || impossible.isSelected()) {
					easy.setSelected(false);
					impossible.setSelected(false);
					difficulty = 1;
				}
				else normal.setSelected(true);
			}
			if (e.getSource()==impossible) {
				if (easy.isSelected() || normal.isSelected()) {
					normal.setSelected(false);
					easy.setSelected(false);
					difficulty = 2;
				}
				else impossible.setSelected(true);
			}
			if(e.getSource()==autoB) {
				int[] info = new int[3];
				int[] shipSize = {5,4,3,3,2};
				boat  = new Ship[5];
				for(int i = 0; i < 5; i++) {
					info = player.randomSetShip(shipSize[i]);
					boat[i] = new Ship(info[0], info[1], info[2], shipSize[i], 1);
				}
				playerBoard.update();
				mainPanel.setVisible(false);
				buttonPanel.setVisible(false);
				pack();
			}
			if(e.getSource()==manualB) {
				boat  = new Ship[5];
				mainPanel.setVisible(false);
				buttonPanel.setVisible(true);
				pack();
			}
			try{
				((JButton)e.getSource()).setEnabled(false);
			}
			catch (ClassCastException c) {}
			if (e.getSource()==carrierB) {
				selectedShip = CARRIER;
				selectedShipSize = 5;
				direction = 1;
				setting = true;
				getEnabled();
			}
			if (e.getSource()==battleshipB) {
				selectedShip = BATTLESHIP;
				selectedShipSize = 4;
				direction = 1;
				setting = true;
				getEnabled();
			}
			if (e.getSource()==submarineB) {
				selectedShip = SUBMARINE;
				selectedShipSize = 3;
				direction = 1;
				setting = true;
				getEnabled();
			}
			if (e.getSource()==destroyerB) {
				selectedShip = DESTROYER;
				selectedShipSize = 3;
				direction = 1;
				setting = true;
				getEnabled();
			}
			if (e.getSource()==patrolB) {
				selectedShip = PATROL;
				selectedShipSize = 2;
				direction = 1;
				setting = true;
				getEnabled();
			}
			
		}
		public void mouseClicked (MouseEvent e) {
			
			if(setting && e.getButton()==MouseEvent.BUTTON1) {
				for(int i = 0; i<10; i++) {
					for(int j = 0; j<10; j++) {
						if(e.getSource()==playerBoard.battleboard[i][j]) {
							if(player.setShip(i, j, direction, selectedShipSize)) {
								playerBoard.update();
								setting = false;
								if(selectedShipSize==2) {
									boat[4] = new Ship(i, j, selectedShipSize, direction, 1);
								}
								else if(selectedShipSize==3) {
									if(!destroyerSet) {
										boat[3] = new Ship(i, j, selectedShipSize, direction, 1);
										destroyerSet = true;
									}
									else{
										boat[2] = new Ship(i, j, selectedShipSize, direction, 1);
									}
								}
								else if(selectedShipSize==4) {
									boat[1] = new Ship(i, j, selectedShipSize, direction, 1);
								}	
								else {
									boat[0] = new Ship(i, j, selectedShipSize, direction, 1);
								}
								
								
							}
						}
					}
				}
			}
			
			if(e.getButton()==MouseEvent.BUTTON3) {
				if(direction==1) {
					direction = 2;
				}
				else {
					direction = 1;
				}
				System.out.println("Direction = " + direction);
				for(int i=0; i<10; i++) {
					for(int j=0; j<10; j++) {
						if(player.checkSquare(i, j) == Board.Square.water.ordinal()) {
							playerBoard.battleboard[i][j].setBackground(Color.blue);
							playerBoard.battleboard[i][j].setIcon(playerBoard.waterIcon);
						}
					} 	
				}
			}
			if(!buttonPanel.isVisible() && !mainPanel.isVisible() && setting == false) {
				for(int i = 0; i < 10; i++) {
					for(int j = 0; j <10; j++) {
						if(e.getSource() == computerBoard.battleboard[i][j]) {
							int shotValue, x, y;
							do {
								shotValue = computer.shootSquare(i, j);
							}while(shotValue == Board.Square.ship.ordinal());
							computerBoard.battleboard[i][j].removeMouseListener(this);
							computerBoard.battleboard[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
							computerBoard.cUpdate();
							boolean callSunk = false;
							scan1:
								for(int k = 0; k < 10; k++) {
									for(int l = 0; l < 10; l++) {
										if(computerBoard.battleboard[k][l].getBackground() == Color.gray) {
											callSunk = true;
											break scan1;
										}
									}
								}
							if(player.checkSquare(hitx, hity) != Board.Square.hit.ordinal()){
								do {
									y = cai.randomlocation();
									x = cai.randomlocation();
									shotValue = player.shootSquare(x,y);
								}while(shotValue == Board.Square.ship.ordinal());
								if(shotValue == Board.Square.hit.ordinal()){
									ohitx = x;
									ohity = y;
									hitx = x;
									hity = y;
								}
								playerBoard.update();
							}
							else {
								try {
									if(player.checkSquare(hitx -1 , hity) != Board.Square.hit.ordinal()
										&&player.checkSquare(hitx-1 , hity) != Board.Square.miss.ordinal()){
									//if(player.checkSquare(hitx-1, hity) != Board.Square.miss.ordinal()){
										shotValue = player.shootSquare(hitx - 1 ,hity);
										playerBoard.update();
										if(shotValue == Board.Square.hit.ordinal()){
											hitx = hitx-1;
										}
										else{
											hitx = ohitx;
										}
									//}
									}
									else if(player.checkSquare(hitx + 1, hity) != Board.Square.hit.ordinal()
											&&player.checkSquare(hitx +1, hity) != Board.Square.miss.ordinal()){
										//if(player.checkSquare(hitx+1, hity) != Board.Square.miss.ordinal()){
											shotValue = player.shootSquare(hitx + 1, hity);
											playerBoard.update();
											if(shotValue == Board.Square.hit.ordinal()){
												hitx = hitx+1;
											}
											else{
												hitx = ohitx;
											}
										//}
									}
									else if(player.checkSquare(hitx, hity + 1) != Board.Square.hit.ordinal()
											&& player.checkSquare(hitx, hity+1) != Board.Square.miss.ordinal()){
										//if(player.checkSquare(hitx, hity+1) != Board.Square.miss.ordinal()){
											shotValue = player.shootSquare(hitx, hity+1);
											playerBoard.update();
											if(shotValue == Board.Square.hit.ordinal()){
												hity = hity +1;
											}
											else{
												hity = ohity;
											}
										//}
									}
									else if (player.checkSquare(hitx, hity -1 ) != Board.Square.hit.ordinal() 
											&& player.checkSquare(hitx, hity-1 ) != Board.Square.miss.ordinal()){
										shotValue = player.shootSquare(hitx, hity-1);
										playerBoard.update();
										if(shotValue == Board.Square.hit.ordinal()){
											hity = hity - 1;
										}
										else {
											do{
												hitx = cai.randomlocation();
												hity = cai.randomlocation();
											}while(player.checkSquare(hitx, hity) == Board.Square.hit.ordinal() 
												|| player.checkSquare(hitx,hity) == Board.Square.miss.ordinal());
										}								
									}
										
								}
								catch(ArrayIndexOutOfBoundsException z) {
									if (hitx -1 == -1){
										hitx = ohitx+1;
										hity = ohity;
										shotValue = player.shootSquare(hitx, hity);
										if(shotValue == Board.Square.hit.ordinal()){
											hitx = hitx;
										}
									}
									else if (hitx + 1 == 10)
									{
										hitx = ohitx;
										hity = ohity + 1;
										shotValue = player.shootSquare(hitx, hity);
										if(shotValue == Board.Square.hit.ordinal()){
											hity = hity +1;
										}
									}
									
									else if (hity + 1 == 10){
										hity = ohity -1;
										shotValue = player.shootSquare(hitx, hity);
										if(shotValue == Board.Square.hit.ordinal()){
											hity = hity-1;
										}
									}
									else if (hity - 1 == -1){
										do{
											ohitx = cai.randomlocation();
											ohity = cai.randomlocation();
											hitx = ohitx;
											hity = ohity;
										}while(player.checkSquare(ohitx, ohity) == Board.Square.hit.ordinal() || player.checkSquare(ohitx,ohity) == Board.Square.miss.ordinal());
										do {
											y = cai.randomlocation();
											x = cai.randomlocation();
											shotValue = player.shootSquare(x,y);
										}while(shotValue == Board.Square.ship.ordinal());
										if(shotValue == Board.Square.hit.ordinal()){
											ohitx = x;
											ohity = y;
											hitx = x;
											hity = y;
										}
									}
									playerBoard.update();
								}
							}
							boolean pallSunk = false;
							scan2:
								for(int k = 0; k < 10; k++) {
									for(int l = 0; l < 10; l++) {
										if(playerBoard.battleboard[k][l].getBackground() == Color.gray) {
											pallSunk = true;
											break scan2;
										}
									}
								}
							if(!pallSunk) {
								int n = JOptionPane.showConfirmDialog(c, "YOU LOSE!!. Would you like to play again?", "Looser!", 0);
									if(n == 0) {
										newGame();
									} 
									else {
										System.exit(0);
									}
							}
							else if(!callSunk) {
								int m = JOptionPane.showConfirmDialog(c, "YOU WIN!!. Would you like to play again?", "Winner!", 0);
									if(m == 0) {
										newGame();
									} 
									else {
										System.exit(0);
									}
							}
						}
					}	
				}
			}
			if (e.getSource()==logoLabel && setting == false) {
				if(swap == 0) {
					for(int i = 0; i < 10; i++) {
						for(int j = 0; j < 10; j++) {
							if(computerBoard.battleboard[i][j].getBackground() == Color.gray) {
								computerBoard.battleboard[i][j].setBorder(BorderFactory.createLineBorder(Color.RED));
							}
						}
					}
					swap = 1;
				}
				else if(swap == 1) {
					for(int i = 0; i < 10; i++) {
						for(int j = 0; j < 10; j++) {
							if(computerBoard.battleboard[i][j].getBackground() == Color.gray) {
								computerBoard.battleboard[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
							}
						}
					}
					swap = 0;
				}
					
			}
		}
		public void mouseEntered (MouseEvent e) {
			JLabel panel = (JLabel)e.getSource();
			panel.setBorder(BorderFactory.createLineBorder(Color.WHITE));
			if(setting) {
				for(int i=0; i<10; i++) {
					for(int j=0; j<10; j++) {
						if(playerBoard.battleboard[i][j] == e.getSource() && direction == 1) {
							for(int col = j;col < 10 && col< (j + selectedShipSize); col++){
								if(playerBoard.battleboard[i][col].getBackground() != Color.gray) {
									playerBoard.battleboard[i][col].setIcon(null);
									playerBoard.battleboard[i][col].setBackground(Color.white);
								}
							}
						}
						else if(playerBoard.battleboard[i][j] == e.getSource()) {
							for(int row = i;row<10 && row< (i + selectedShipSize); row++) {
								if(playerBoard.battleboard[row][j].getBackground() != Color.gray) {
									playerBoard.battleboard[row][j].setIcon(null);
									playerBoard.battleboard[row][j].setBackground(Color.white);
								}
							}
						}
					}
				}
			}
		}
		public void mouseExited (MouseEvent e) {
			JLabel panel = (JLabel)e.getSource();
			panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
			if(setting) {
				for(int i=0; i<10; i++) {
					for(int j=0; j<10; j++) {
						if(playerBoard.battleboard[i][j] == e.getSource() && direction == 1) {
							for(int col = j;col<10 && col< (j + selectedShipSize); col++){
								if(playerBoard.battleboard[i][col].getBackground() != Color.gray) {
									playerBoard.battleboard[i][col].setBackground(Color.blue);
									playerBoard.battleboard[i][col].setIcon(playerBoard.waterIcon);
								}
							}
						}
						else if(playerBoard.battleboard[i][j] == e.getSource()) {
							for(int row = i;row<10 && row< (i + selectedShipSize); row++) {
								if(playerBoard.battleboard[row][j].getBackground() != Color.gray) {
									playerBoard.battleboard[row][j].setIcon(playerBoard.waterIcon);
									playerBoard.battleboard[row][j].setBackground(Color.blue);
								}
							}
						}
					}
				}
			}
		}
		public void mousePressed (MouseEvent e) {}
		public void mouseReleased (MouseEvent e) {}
		
		public void getEnabled() {
			if(carrierB.isEnabled() || battleshipB.isEnabled() || submarineB.isEnabled() || destroyerB.isEnabled() || patrolB.isEnabled()) {
				buttonPanel.setVisible(true);
				pack();
			}
			else {
				buttonPanel.setVisible(false);
				pack();
			}
		}
	}
}

